package com.frt.sygang.sevice.impl;/**
 * Created by 冯瑞涛 on 2022/9/18 18:48
 */

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.frt.sygang.common.CustomException;
import com.frt.sygang.sevice.DishService;
import com.frt.sygang.sevice.SetmealDishService;
import com.frt.sygang.entity.dto.DishDto;
import com.frt.sygang.entity.Dish;
import com.frt.sygang.entity.DishFlavor;
import com.frt.sygang.entity.SetmealDish;
import com.frt.sygang.mapper.DishMapper;
import com.frt.sygang.sevice.DishFlavorService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 冯瑞涛
 * @date 2022/9/18 18:48
 * @version 1.0
 */
@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {

    @Autowired
    private DishFlavorService flavorService;

    @Autowired
    private SetmealDishService setmealDishService;

    //新增菜品方法
    @Override
    @Transactional //事物管理注解
    public void insDishAndFlavor(DishDto dishDto) {
        //执行菜品的添加方法
        this.save(dishDto);
        //将参数的id取出作为菜品id
        Long dishId = dishDto.getId();
        //将参数的口味集合取出
        List<DishFlavor> flavors = dishDto.getFlavors();
        //通过stream流的方式将菜品id赋值给遍历之后的口味对象的dishId属性
        flavors=flavors.stream().map(temp->{
           temp.setDishId(dishId);
           return temp;
        }).collect(Collectors.toList());
        //执行添加口味方法
        flavorService.saveBatch(flavors);
    }

    /**
     * 菜品和相关口味信息回显
     * @param id
     * @return
     */
    @Override
    public DishDto backDishDto(Long id) {
        //声明一个菜品数据传输对象
        DishDto dishDto=new DishDto();
        //新建一个口味查询条件对象
        LambdaQueryWrapper<DishFlavor> queryWrapper=new LambdaQueryWrapper<>();
        //添加根据菜品id查询口味集合的条件
        queryWrapper.eq(DishFlavor::getDishId,id);
        //调用根据id查询菜品信息方法
        Dish dish = this.getById(id);
        //根据上方创建的条件对象中的条件查询口味集合
        List<DishFlavor> list = flavorService.list(queryWrapper);
        //将查询出的菜品信息中属性的值复制给数据传输对象
        BeanUtils.copyProperties(dish,dishDto);
        //如果口味集合不为空则将口味集合赋值给数据传输对象
        if(list!=null&&!list.isEmpty()){
            dishDto.setFlavors(list);
        }
        return dishDto;
    }

    /**
     *更新菜品和相关口味信息
     * @param dishDto
     */
    @Override
    @Transactional
    public void setDishAndFlavor(DishDto dishDto) {
        //1.修改菜品信息
        this.updateById(dishDto);
        //删除原有口味信息
        LambdaQueryWrapper<DishFlavor> queryWrapper=new LambdaQueryWrapper<>();
        Long dishId = dishDto.getId();
        queryWrapper.eq(DishFlavor::getDishId,dishId);
        flavorService.remove(queryWrapper);
        //添加新的口味信息
        List<DishFlavor> flavors = dishDto.getFlavors();
        flavors=flavors.stream().map(temp->{
            temp.setDishId(dishId);
            return temp;
        }).collect(Collectors.toList());
        flavorService.saveBatch(flavors);
    }

    /**
     * 批量删除方法
     * @param ids
     */
    @Override
    @Transactional
    public void deleteDish(List<Long> ids) {
        LambdaQueryWrapper<Dish> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.in(Dish::getId,ids);
        queryWrapper.eq(Dish::getStatus,1);
        int count = this.count(queryWrapper);
        if(count>0){
            throw new CustomException("菜品为在售菜品，无法删除");
        }
        LambdaQueryWrapper<SetmealDish> wrapper=new LambdaQueryWrapper<>();
        wrapper.in(SetmealDish::getDishId,ids);
        int steSum = setmealDishService.count(wrapper);
        if(steSum>0){
            throw new CustomException("该菜品绑定套餐，无法删除");
        }
        this.removeByIds(ids);
        LambdaUpdateWrapper<DishFlavor> updateWrapper=new LambdaUpdateWrapper<>();
        updateWrapper.in(DishFlavor::getDishId,ids);
        flavorService.remove(updateWrapper);
    }

}
